#include "systemcmddelegate.h"
#include <QDateTime>
#include <QThread>
#include "hmicommondefine.h"
#include "instructionEnum.h"

//#include <QDebug>

SystemCmdDelegate::SystemCmdDelegate()
{
}

void SystemCmdDelegate::systemCmdRestore(QString cmd)
{
    emit msgBoxSignal(EM_MSG_BOX_NO_BTN,tr("提示"),tr("正在还原"));
    QString tmp="unzip -o "+cmd+" -d /";
    //std::string s = tmp.toStdString();
    //const char* c = s.c_str();
    QByteArray tmpCmdArray = tmp.toLatin1();
    if(0==system(tmpCmdArray.data()))
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("还原成功"));
    }
    else
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("还原失败"));
    }
}

void SystemCmdDelegate::systemCmdBackupAll(QString cmd)
{
    emit msgBoxSignal(EM_MSG_BOX_NO_BTN,tr("提示"),tr("正在备份中,请稍后"));
    //qDebug()<<"QThread::currentThreadId() systemCmdBackupAll:"<<QThread::currentThreadId();
    QDateTime t = QDateTime::currentDateTime();
    QString time = t.toString("yyyyMMddhhmmss");
    QString tmp="zip -r "+cmd+"/backup_data_"+time+".zip"+" /agvwork/AgvFundation/fundation/data";
    //std::string s = tmp.toStdString();
    //const char* c = s.c_str();
    QByteArray tmpCmdArray = tmp.toLatin1();
    if(0==system(tmpCmdArray.data()))
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("备份成功"));
    }
    else
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("备份失败"));
    }
}

void SystemCmdDelegate::systemCmdBackupGlobal(QString cmd)
{
    emit msgBoxSignal(EM_MSG_BOX_NO_BTN,tr("提示"),tr("正在备份中,请稍后"));
    QDateTime t = QDateTime::currentDateTime();
    QString time = t.toString("yyyyMMddhhmmss");
    QString tmp="zip -r "+cmd+"/backup_globalConfig_"+time+".zip"+" /agvwork/AgvFundation/fundation/data/globalConfig";
    //std::string s = tmp.toStdString();
    //const char* c = s.c_str();
    QByteArray tmpCmdArray = tmp.toLatin1();
    if(0==system(tmpCmdArray.data()))
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("备份成功"));
    }
    else
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("备份失败"));
    }
}

void SystemCmdDelegate::systemCmdBackupProgram(QString cmd,int n)
{
    emit msgBoxSignal(EM_MSG_BOX_NO_BTN,tr("提示"),tr("正在备份中,请稍后"));
    QDateTime t = QDateTime::currentDateTime();
    QString time = t.toString("yyyyMMddhhmmss");
    QString l=QString::number(n+1);
    QString tmp="zip -r "+cmd+"/robotData"+l+"_program_"+time+".zip"+" /agvwork/AgvFundation/fundation/data/robotData"+l+"/program";
    //std::string s = tmp.toStdString();
    //const char* c = s.c_str();
    QByteArray tmpCmdArray = tmp.toLatin1();
    if(0==system(tmpCmdArray.data()))
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("备份成功"));
    }
    else
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("备份失败"));
    }
}

void SystemCmdDelegate::systemCmdBackupDat(QString cmd,int n)
{
    emit msgBoxSignal(EM_MSG_BOX_NO_BTN,tr("提示"),tr("正在备份中,请稍后"));
    QDateTime t = QDateTime::currentDateTime();
    QString time = t.toString("yyyyMMddhhmmss");
    QString l=QString::number(n+1);
    QString tmp="zip -r "+cmd+"/robotData"+l+"_dat_"+time+".zip"+" /agvwork/AgvFundation/fundation/data/robotData"+l+"/dat";
    //std::string s = tmp.toStdString();
    //const char* c = s.c_str();
    QByteArray tmpCmdArray = tmp.toLatin1();
    if(0==system(tmpCmdArray.data()))
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("备份成功"));
    }
    else
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("备份失败"));
    }
}

void SystemCmdDelegate::systemCmdBackupConfig(QString cmd,int n)
{
    emit msgBoxSignal(EM_MSG_BOX_NO_BTN,tr("提示"),tr("正在备份中,请稍后"));
    QDateTime t = QDateTime::currentDateTime();
    QString time = t.toString("yyyyMMddhhmmss");
    QString l=QString::number(n+1);
    QString tmp="zip -r "+cmd+"/robotData"+l+"_config_"+time+".zip"+" /agvwork/AgvFundation/fundation/data/robotData"+l+"/config";
    //std::string s = tmp.toStdString();
    //const char* c = s.c_str();

    QByteArray tmpCmdArray = tmp.toLatin1();
    if(0==system(tmpCmdArray.data()))
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("备份成功"));
    }
    else
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("备份失败"));
    }
}
